package com.pinyoumei.app.activity.userpersonal;

import android.support.annotation.NonNull;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.StaggeredGridLayoutManager;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.bumptech.glide.Glide;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.lzy.okgo.OkGo;
import com.lzy.okgo.callback.StringCallback;
import com.lzy.okgo.model.Response;
import com.lzy.okgo.request.base.Request;
import com.pinyoumei.app.R;
import com.pinyoumei.app.activity.BaseActivityCurrency;
import com.pinyoumei.app.adapter.UserBalanceAdapter;
import com.pinyoumei.app.bean.FourBean;
import com.pinyoumei.app.network.Constants;
import com.pinyoumei.app.utils.SPUtils;
import com.pinyoumei.app.utils.SignatureUtils;
import com.pinyoumei.app.utils.Utils;
import com.pinyoumei.app.view.CircularImageView;
import com.scwang.smartrefresh.layout.SmartRefreshLayout;
import com.scwang.smartrefresh.layout.api.RefreshLayout;
import com.scwang.smartrefresh.layout.listener.OnLoadMoreListener;
import com.scwang.smartrefresh.layout.listener.OnRefreshListener;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class MyPerformanceActivity extends BaseActivityCurrency implements View.OnClickListener {

    private CircularImageView mpvOneIcon,mpvTwoIcon,mpvThreeIcon;
    private TextView mpvOneName,mpvOneTxt,mpvTwoName,mpvTwoTxt,mpvThreeName,mpvThreeTxt,mpvRanking,mpvRankingPer,mpvBtn1,mpvBtn2;
    private RecyclerView mpvRecycler;
    private MyPerformanceAdapter myPerformanceAdapter;
    private int page = 1;
    private String flag = "0";
    private List<MyPerformanceBean.ResultBean.MyPerformanceDto> performanceDtoList = new ArrayList<>();

    @Override
    protected int layoutId() {
        return R.layout.my_performance_view;
    }

    @Override
    protected void initView() {
        mpvOneIcon  = findViewById(R.id.mpv_one_icon);
        mpvTwoIcon = findViewById(R.id.mpv_two_icon);
        mpvThreeIcon = findViewById(R.id.mpv_three_icon);
        mpvOneName = findViewById(R.id.mpv_one_name);
        mpvOneTxt = findViewById(R.id.mpv_one_txt);
        mpvTwoName = findViewById(R.id.mpv_two_name);
        mpvTwoTxt = findViewById(R.id.mpv_two_txt);
        mpvThreeName = findViewById(R.id.mpv_three_name);
        mpvThreeTxt = findViewById(R.id.mpv_three_txt);
        mpvRanking = findViewById(R.id.mpv_ranking);
        mpvRankingPer = findViewById(R.id.mpv_ranking_per);
        mpvRecycler = findViewById(R.id.mpv_recycler);
        mpvBtn1 = findViewById(R.id.mpv_btn1);
        mpvBtn1.setOnClickListener(this);
        mpvBtn2 = findViewById(R.id.mpv_btn2);
        mpvBtn2.setOnClickListener(this);


        mpvRecycler.setLayoutManager(new StaggeredGridLayoutManager(1,StaggeredGridLayoutManager.VERTICAL));
        myPerformanceAdapter = new MyPerformanceAdapter(this);
        mpvRecycler.setAdapter(myPerformanceAdapter);

    }

    @Override
    protected void initData() {
        getTopInfo(flag);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.mpv_btn1:
                page = 1;
                flag = "0";
                initBtn(mpvBtn1);
                getTopInfo(flag);
                break;
            case R.id.mpv_btn2:
                page = 1;
                flag = "1";
                initBtn(mpvBtn2);
                getTopInfo(flag);
                break;
        }
    }

    private void initBtn(TextView textView){
        mpvBtn1.setBackgroundResource(R.drawable.radius14_color_363c77_bg);
        mpvBtn2.setBackgroundResource(R.drawable.radius14_color_363c77_bg);
        textView.setBackgroundResource(R.mipmap.performance_btn1);
    }

    //返回
    public void onBackListener(View view){
        finish();
    }

    private void getTopInfo(String flag) {
        Map<String, Object> map = new HashMap<>();
        map.put("user_id", SPUtils.getAppUserId(MyPerformanceActivity.this));
        map.put("yesterday_flag",flag);
        OkGo.<String>post(Constants.getTopInfo)
                .params("user_id", SPUtils.getAppUserId(MyPerformanceActivity.this))
                .params("yesterday_flag", flag)
                .params("sign", SignatureUtils.createSignature(map, Constants.SIGN_SECRET))
                .execute(new StringCallback() {




                    @Override
                    public void onStart(Request<String, ? extends Request> request) {
                        super.onStart(request);
                        loadingDailog.show();
                    }

                    @Override
                    public void onCacheSuccess(Response<String> response) {
                        super.onCacheSuccess(response);
                        setData(response);
                    }

                    @Override
                    public void onSuccess(Response<String> response) {
                        setData(response);
                    }

                    @Override
                    public void onError(Response<String> response) {
                        super.onError(response);
                        loadingDailog.dismiss();
                    }

                    private void setData(Response<String> response) {
                        loadingDailog.dismiss();
                        MyPerformanceBean myPerformanceBean = gson.fromJson(response.body(), MyPerformanceBean.class);

                        if (myPerformanceBean.getCode() >= 0) {

                            mpvRanking.setText(myPerformanceBean.getResult().getMyTopInfo().getTop());
                            mpvRankingPer.setText(myPerformanceBean.getResult().getMyTopInfo().getTeamAmt());

                            if (page == 1){
                                performanceDtoList.clear();
                            }
                            performanceDtoList.addAll(myPerformanceBean.getResult().getTopInfo());
                            myPerformanceAdapter.replaceAll(performanceDtoList);
                            myPerformanceAdapter.notifyDataSetChanged();
//                            setOneTwoThree();
                        } else {
                            Toast.makeText(MyPerformanceActivity.this, myPerformanceBean.getMsg(), Toast.LENGTH_SHORT).show();
                        }

                    }

                    private void setOneTwoThree(){
                        if(performanceDtoList.size() == 1){
                            Glide.with(MyPerformanceActivity.this).load(performanceDtoList.get(0).getUserHeadImg()).into(mpvOneIcon);
                            mpvOneName.setText(performanceDtoList.get(0).getUserName());
                            mpvOneTxt.setText(performanceDtoList.get(0).getTeamAmt());
                        }else if(performanceDtoList.size() == 2) {
                            Glide.with(MyPerformanceActivity.this).load(performanceDtoList.get(0).getUserHeadImg()).into(mpvOneIcon);
                            mpvOneName.setText(performanceDtoList.get(0).getUserName());
                            mpvOneTxt.setText(performanceDtoList.get(0).getTeamAmt());
                            Glide.with(MyPerformanceActivity.this).load(performanceDtoList.get(1).getUserHeadImg()).into(mpvTwoIcon);
                            mpvTwoName.setText(performanceDtoList.get(1).getUserName());
                            mpvTwoTxt.setText(performanceDtoList.get(1).getTeamAmt());
                        }else if(performanceDtoList.size() >= 3) {
                            Glide.with(MyPerformanceActivity.this).load(performanceDtoList.get(0).getUserHeadImg()).into(mpvOneIcon);
                            mpvOneName.setText(performanceDtoList.get(0).getUserName());
                            mpvOneTxt.setText(performanceDtoList.get(0).getTeamAmt());
                            Glide.with(MyPerformanceActivity.this).load(performanceDtoList.get(1).getUserHeadImg()).into(mpvTwoIcon);
                            mpvTwoName.setText(performanceDtoList.get(1).getUserName());
                            mpvTwoTxt.setText(performanceDtoList.get(1).getTeamAmt());
                            Glide.with(MyPerformanceActivity.this).load(performanceDtoList.get(2).getUserHeadImg()).into(mpvThreeIcon);
                            mpvThreeName.setText(performanceDtoList.get(2).getUserName());
                            mpvThreeTxt.setText(performanceDtoList.get(2).getTeamAmt());
                        }

                        List<MyPerformanceBean.ResultBean.MyPerformanceDto> repList = new ArrayList<>();
                        for(int i=0;i<performanceDtoList.size();i++){
                            if(i > 2){
                                MyPerformanceBean.ResultBean.MyPerformanceDto dto = performanceDtoList.get(i);
                                repList.add(dto);
                            }
                        }

                        myPerformanceAdapter.replaceAll(repList);
                        myPerformanceAdapter.notifyDataSetChanged();
                    }

                });
    }


}
